<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺" prop="shopIds">
        <el-cascader
          v-model="queryParams.shopIds"
          :options="options.deptOptions"
          filter clearable
          placeholder="请选择店铺"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="商品编码" prop="gNumber">
        <el-input
          v-model="queryParams.gNumber"
          placeholder="请输入商品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上架状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择上架状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_goods_sj_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.delFlag" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入库时间">
        <el-date-picker
          v-model="queryParams.params.startTime"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetime"
          placeholder="选择开始时间">
        </el-date-picker>
        <span> ~ </span>
        <el-date-picker
          v-model="queryParams.params.endTime"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetime"
          placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['goods:shop:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['goods:shop:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['goods:shop:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['goods:shop:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-caret-top"
          size="mini"
          :disabled="multiple"
          @click="handlePutOn"
          v-hasPermi="['goods:shop:edit']"
        >一键上架</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-caret-bottom"
          size="mini"
          :disabled="multiple"
          @click="handlePutOut"
          v-hasPermi="['goods:shop:edit']"
        >一键下架</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border stripe fit
              v-loading="loading" :data="shopList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="店铺" align="center" prop="deptName" />
      <el-table-column label="商品编码" align="center" prop="gNumber" width="220" :show-overflow-tooltip="true" />
      <el-table-column label="商品名称" align="center" prop="goodsName" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          <el-link type="primary" @click="handleGoodsNameClick(scope.row)">{{scope.row.goodsName}}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="商品封面" align="center" prop="image" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :height="40" />
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center" prop="price" >
        <template slot-scope="scope">
          <span v-if="scope.row.price" >{{ scope.row.price/100 }} ￥</span>
        </template>
      </el-table-column>
      <el-table-column label="库存" align="center" prop="stockQuantity" />
      <el-table-column label="上架状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_goods_sj_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="顺序" align="center" prop="orderNum" />
      <el-table-column label="状态" align="center" prop="delFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.delFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="入库时间" align="center" prop="createTime" width="180" />
      <el-table-column label="操作者" align="center" prop="updateBy" />
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="110">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['goods:shop:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            style="color: #f56c6c"
            v-hasPermi="['goods:shop:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改店铺商品关联对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" :close-on-click-modal="false" >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="18">
            <el-form-item label="店铺" prop="deptId" >
              <el-cascader
                v-model="form.deptIds"
                :options="options.deptOptions"
                :show-all-levels="true"
                filter clearable disabled
                placeholder="请选择店铺"
                style="width: 100%"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="顺序" prop="orderNum">
              <el-input v-model="form.orderNum" type="number" placeholder="请输入顺序" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="商品编码" prop="gNumber">
          <el-input v-model="form.gNumber" placeholder="请输入商品编码" disabled />
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsName" disabled>
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" disabled />
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="价格" prop="price">
              <el-input v-model="form.price" type="number" placeholder="请输入价格" >
                <template slot="append">分</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="库存" prop="stockQuantity">
              <el-input v-model="form.stockQuantity" type="number" placeholder="请输入库存" ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="上架状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择上架状态" >
                <el-option
                  v-for="dict in dict.type.sys_goods_sj_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态" prop="delFlag">
              <el-radio-group v-model="form.delFlag">
                <el-radio
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :rows="5" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <shop-goods-add ref="addShopGoods" @success="handleAddSuccess" />

    <el-dialog :title="dialogInfo.title" :visible.sync="dialogInfo.open" width="1150px" append-to-body>
      <goods-item :gNumber="dialogInfo.gNumber" :dept-id="dialogInfo.deptId"></goods-item>
    </el-dialog>
  </div>
</template>

<script>
import { listShop, getShop, delShop, addShop, updateShop, shopTreeSelect, putOnShops, putOutShops } from "@/api/goods/shop";
import Treeselect from '@riophae/vue-treeselect'
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

import ShopGoodsAdd from '@/views/goods/shop/add.vue'
import GoodsItem from '@/views/goods/list/item.vue'

export default {
  name: "Shop",
  components: { GoodsItem, Treeselect, ShopGoodsAdd },
  dicts: ['sys_normal_disable', 'sys_goods_sj_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 店铺商品关联表格数据
      shopList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      options: {
        deptOptions: []
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopIds: undefined,
        deptId: undefined,
        gNumber: undefined,
        goodsName: undefined,
        status: undefined,
        delFlag: undefined,
        params: {
          startTime: undefined,
          endTime: undefined,
        },
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        'stataus': [
          { required: true, message: "上架状态不能为空", trigger: "blur" }
        ],
        'delFlag': [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
      },
      dialogInfo: {
        title: "",
        open: false,
        gNumber: undefined,
        deptId: undefined,
      },
    };
  },
  created() {
    this.shopTreeSelect();
    this.getList();
  },
  methods: {
    /** 查询部门下拉树结构 */
    shopTreeSelect() {
      shopTreeSelect().then(res => {
        let list = res.data;
        this.options.deptOptions = list;
      });
    },
    /** 查询店铺商品关联列表 */
    getList() {
      this.loading = true;
      this.queryParams.deptId = null
      if (this.queryParams.shopIds && this.queryParams.shopIds.length > 0) {
        this.queryParams.deptId = this.queryParams.shopIds[this.queryParams.shopIds.length-1];
      }
      listShop(this.queryParams).then(response => {
        this.shopList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        gshopId: null,
        deptId: null,
        gNumber: null,
        price: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.params.startTime = undefined;
      this.queryParams.params.endTime = undefined;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.gshopId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加店铺商品关联";
      this.$refs.addShopGoods.show();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const gshopId = row.gshopId || this.ids
      getShop(gshopId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改店铺商品关联";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.gshopId != null) {
            updateShop(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShop(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const gshopIds = row.gshopId || this.ids;
      this.$modal.confirm('是否确认删除店铺商品关联编号为"' + gshopIds + '"的数据项？').then(function() {
        return delShop(gshopIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('goods/shop/export', {
        ...this.queryParams
      }, `shop_${new Date().getTime()}.xlsx`)
    },
    handleGoodsNameClick(row) {
      this.dialogInfo.title = row.goodsName
      this.dialogInfo.gNumber = row.gNumber
      this.dialogInfo.deptId = row.deptId
      this.dialogInfo.open = true
    },
    handleAddSuccess() {
      this.handleQuery();
    },
    handlePutOn() {
      const gshopIds = this.ids;
      if (gshopIds) {
        this.$modal.confirm('是否确认 上架 选择的商品？').then(function() {
          return putOnShops(gshopIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("上架成功");
        }).catch(() => {
        });
      } else {
        this.$modal.msgError("未选择商品")
      }
    },
    handlePutOut() {
      const gshopIds = this.ids;
      if (gshopIds) {
        this.$modal.confirm('是否确认 下架 选择的商品？').then(function() {
          return putOutShops(gshopIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("下架成功");
        }).catch(() => {
        });
      } else {
        this.$modal.msgError("未选择商品")
      }
    }
  }
};
</script>

<style scoped>
::v-deep .el-tooltip__popper {
  width: 300px;
}
</style>
